﻿using System;
using System.Linq;
using Algoritmia.DataStructures.Lists;
using Algoritmia.DataStructures.Maps;

namespace Algoritmia.Problems.TextProcessing
{
    public class Hyphenator
    {
        private readonly IMap<int, IMap<string, IList<int>>> _table;

        public Hyphenator(IMap<int,IMap<string,IList<int>>> table)
        {
            _table = table;
        }

        public string Hyphenate(string word)
        {
            var mark = "." + word + ".";
            var value = Enumerable.Repeat(0, word.Length + 2).ToArray();
            for(int i = 0; i <= mark.Length; i++)
            {
                for(int j = i+2; j <= mark.Length; j++)
                {
                    if ( _table.Contains(j-i) && _table[j-i].Contains(mark.Substring(i,j-i)))
                    {
                        var valuesList = _table[j - i][mark.Substring(i, j - i)];
                        for(int v = 0; v < valuesList.Count; v++)
                        {
                            value[i - 1 + v] = Math.Max(value[i - 1 + v], valuesList[v]);
                        }
                    }
                }
            }
            return string.Join("", Enumerable.Range(1, value.Length - 2).Select(i => mark[i] + (value[i]%2 == 1 ? "-" : "")));
        }

    }
}
